package com.molichuxing.services.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.business.convertor.Convert;
import com.molichuxing.services.business.dto.response.StockRecordBizDto;
import com.molichuxing.services.business.service.StockRecordBizService;
import com.molichuxing.services.infrastructure.dto.response.EntrepotStorageDto;
import com.molichuxing.services.infrastructure.dto.response.StockCarDto;
import com.molichuxing.services.infrastructure.dto.response.StockRecordDto;
import com.molichuxing.services.infrastructure.dto.response.StockStoreDto;
import com.molichuxing.services.infrastructure.service.EntrepotStorageService;
import com.molichuxing.services.infrastructure.service.StockCarService;
import com.molichuxing.services.infrastructure.service.StockRecordService;
import com.molichuxing.services.infrastructure.service.StockStoreService;
import com.molichuxing.services.property.StockCarStatusEnum;
import com.molichuxing.services.property.StockCarStockTypeEnum;
import com.molichuxing.services.property.StockCarTypeEnum;
import com.molichuxing.services.property.StockRecordStatusEnum;
import com.molichuxing.services.property.StockRecordTypeEnum;

/**
 * 
 * @author liyg
 * 库存物流组合服务实现类
 * 2019-08-21
 *
 */
@Service("stockRecordBizService")
public class StockRecordBizServiceImpl implements StockRecordBizService{

	@Autowired
	private StockRecordService stockRecordService;
	
	@Autowired
	private StockCarService stockCarService;
	
	@Autowired
	private EntrepotStorageService entrepotStorageService;
	
	@Autowired
	private StockStoreService stockStoreService;
	
	/**
	 *获取库存记录列表
	 * @param map 参数
	 *key:carId      Integer 车辆id 非必填
	 *key:vehicleId  Integer 车型id 非必填
	 *key:type       Integer 出入库类型 非必填
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockRecordBizDto> getStockRecordList(Map<String, Object> map) {
		
		Paged<StockRecordDto> paged = stockRecordService.getStockRecordList(map);
		if(null == paged) {
			return null;
		}else if(paged.isEmpty()) {
			return new Paged<>(null,paged.getTotal(),paged.getPageSize());
		}
		
		List<StockRecordBizDto> stockRecordBizDtoList = Convert.toStockRecordBizDto(paged.getList());
		//获取仓库名称
		getStockName(stockRecordBizDtoList);
	   
		Long total=paged.getTotal();
		Integer pageSize=paged.getPageSize();
		Paged<StockRecordBizDto> page=new Paged<>(stockRecordBizDtoList,total,pageSize);
		
		return page;
	}
	
	/**
	 * 获取仓库名称
	 * @param stockRecordBizDtoList
	 */
	private void getStockName(List<StockRecordBizDto> stockRecordBizDtoList) {
		 
		stockRecordBizDtoList.forEach(dto ->{
	    	/*发车库*/
	    	Integer outStockType = dto.getOutStockType();
	    	Integer outStockId = dto.getOutStockId();
	    	if(null != outStockType && null != outStockId) {
	    		if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == outStockType ) {
					/*查询中转库名称*/
					EntrepotStorageDto entrepotStorage = entrepotStorageService.getEntrepotStorage(outStockId);
					if(null != entrepotStorage) {
						dto.setOutStockName(entrepotStorage.getShortName());
					}
				}else {
					/*查询门店id*/
					StockStoreDto stockStore = stockStoreService.getStockStore(outStockId);
					if(null != stockStore) {
						dto.setOutStoreId(stockStore.getStoreId());
					}
				}	
	    	}
			
			/*收车库*/
	    	Integer inStockType = dto.getInStockType();
	    	Integer inStockId = dto.getInStockId();
			if(null != inStockType && null != inStockId) {
				if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == inStockType) {
					/*查询中转库名称*/
					EntrepotStorageDto entrepotStorage = entrepotStorageService.getEntrepotStorage(inStockId);
					if(null != entrepotStorage) {
						dto.setInStockName(entrepotStorage.getShortName());
					}
				}else {
					/*查询门店id*/
					StockStoreDto stockStore = stockStoreService.getStockStore(inStockId);
					if(null != stockStore) {
						dto.setInStoreId(stockStore.getStoreId());
					}
				}
			}
		});
	    
	}
	
	/**
	 * 根据车辆id获取库存历史流转记录
	 * @param carId
	 * @return
	 */
	@Override
	public List<StockRecordBizDto> getListByCarId(Integer carId) {
		
		StockCarDto stockCar = stockCarService.getStockCar(carId);
		if(null == stockCar) {
			return null;
		}
		
		List<StockRecordBizDto> stockRecordBizDtoList =null;
		if(StockCarStatusEnum.WAIT_STOCK.getValue() == stockCar.getStatus()
				&& StockCarTypeEnum.PURCHASE.getValue() == stockCar.getType()) {
			stockRecordBizDtoList=new ArrayList<>();
			stockRecordBizDtoList.add(Convert.toStockRecordBizDto(stockCar));
			stockRecordBizDtoList.add(getStockRecordBizDto());
			return stockRecordBizDtoList;
		}
		
		List<StockRecordDto> list = stockRecordService.getListByCarId(carId);
		stockRecordBizDtoList = Convert.toStockRecordBizDto(list);
		if(null == stockRecordBizDtoList) {
			stockRecordBizDtoList=new ArrayList<>();
		}
		//获取当前库存的状态的数据
		getStockCar(stockRecordBizDtoList,stockCar);
		
		//获取仓库名称
		getStockName(stockRecordBizDtoList);
		for (StockRecordBizDto dto : stockRecordBizDtoList) {
			if(StockRecordTypeEnum.PURCHASE_IN_STOCK.getValue() == dto.getType()) {
				dto.setOutStockName("采购完成");
			}
		}
		
		return stockRecordBizDtoList;
	}
	
	/**
	 * 获取当前库存的状态的数据
	 * @param list
	 * @param stockCar
	 */
	private void getStockCar(List<StockRecordBizDto> list,StockCarDto stockCar) {
		
		StockRecordBizDto stockRecordBizDto = Convert.toStockRecordBizDto(stockCar);
		stockRecordBizDto.setStatus(StockRecordStatusEnum.IN_STOCK.getValue());
		stockRecordBizDto.setOperationTime(null);
		stockRecordBizDto.setAccountAccount(null);
		
		Integer type = stockCar.getType();
		Integer status = stockCar.getStatus();
		
		if(StockCarTypeEnum.ALLOT.getValue() == type && StockCarStatusEnum.WAIT_STOCK.getValue() ==status) {
			stockRecordBizDto.setStatus(StockRecordStatusEnum.WAIT_STOCK.getValue());
			stockRecordBizDto.setType(StockRecordTypeEnum.ALLOT_IN_STOCK.getValue());
			Integer inStockType = stockCar.getInStockType();
			if(StockCarStockTypeEnum.STORE.getValue() == inStockType) {
				stockRecordBizDto.setType(StockRecordTypeEnum.ALLOT_IN_STORE.getValue());
			}
			list.add(stockRecordBizDto);
		}else if(StockCarTypeEnum.COLLECT.getValue() == type && StockCarStatusEnum.WAIT_STOCK.getValue() ==status) {
			stockRecordBizDto.setStatus(StockRecordStatusEnum.WAIT_STOCK.getValue());
			stockRecordBizDto.setType(StockRecordTypeEnum.COLLECT_IN_STOCK.getValue());
			list.add(stockRecordBizDto);
		}
		
		if(list.isEmpty()) {
			if(StockCarTypeEnum.ALLOT.getValue() == type ) {
				stockRecordBizDto.setType(StockRecordTypeEnum.ALLOT_IN_STOCK.getValue());
				Integer inStockType = stockCar.getInStockType();
				if(StockCarStockTypeEnum.STORE.getValue() == inStockType) {
					stockRecordBizDto.setType(StockRecordTypeEnum.ALLOT_IN_STORE.getValue());
				}
				list.add(stockRecordBizDto);
			}else if(StockCarTypeEnum.COLLECT.getValue() == type) {
				stockRecordBizDto.setType(StockRecordTypeEnum.COLLECT_IN_STOCK.getValue());
				list.add(stockRecordBizDto);
			}else if(StockCarTypeEnum.PURCHASE.getValue() == type) {
				stockRecordBizDto.setType(StockRecordTypeEnum.PURCHASE_IN_STOCK.getValue());
				stockRecordBizDto.setOperationTime(stockCar.getStorageTime());
				stockRecordBizDto.setAccountAccount(stockCar.getAccountAccount());
				list.add(stockRecordBizDto);
			}
		}
		/*库存状态不是收车状态时 需要补充一个收车入库记录  并且是该条记录不能高亮显示*/
		if(StockCarTypeEnum.COLLECT.getValue() != type) {
			StockRecordBizDto bizDto = getStockRecordBizDto();
			list.add(bizDto);
		}
		
	}
	
	/**
	 * 获取收车入库记录
	 * @return
	 */
	private StockRecordBizDto getStockRecordBizDto() {
		StockRecordBizDto bizDto=new StockRecordBizDto();
		bizDto.setStatus(StockRecordStatusEnum.WAIT_STOCK.getValue());
		bizDto.setType(StockRecordTypeEnum.COLLECT_IN_STOCK.getValue());
		bizDto.setOutStockType(StockCarStockTypeEnum.STORE.getValue());
		bizDto.setInStockType(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue());
		bizDto.setIsShow(false);
		return bizDto;
	}

	/**
	 *获取库存记录列表(已回库)
	 * @param map 参数
	 *key:carId      Integer 车辆id 非必填
	 *key:stockIds   List 仓库ids
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockRecordBizDto> getReturnStockCarList(Map<String, Object> map) {
		
		Paged<StockRecordDto> paged = stockRecordService.getReturnStockCarList(map);
		if(null == paged) {
			return null;
		}else if(paged.isEmpty()) {
			return new Paged<>(null,paged.getTotal(),paged.getPageSize());
		}
		
		List<StockRecordBizDto> stockRecordBizDtoList = Convert.toStockRecordBizDto(paged.getList());
		//获取仓库名称
		getStockName(stockRecordBizDtoList);
	   
		Long total=paged.getTotal();
		Integer pageSize=paged.getPageSize();
		Paged<StockRecordBizDto> page=new Paged<>(stockRecordBizDtoList,total,pageSize);
		return page;
	}
	
	/**
	 * 根据车辆id获取库存历史流转记录
	 * @param carId
	 * @param storeIds
	 * @return
	 */
	@Override
	public List<StockRecordBizDto> getListByCarId(Integer carId, List<Integer> storeIds) {
		
		List<Integer> stockIds = stockStoreService.getStockIds(storeIds);
		if(null == stockIds || stockIds.isEmpty()) {
			throw new  BizException(400, "参数异常");
		}
		StockCarDto stockCar = stockCarService.getStockCar(carId,stockIds);
		if(null == stockCar) {
			throw new BizException(400, "参数异常");
		}
		
		List<StockRecordDto> list = stockRecordService.getListByCarId(carId,stockIds);
		List<StockRecordBizDto> bizDtoList= Convert.toStockRecordBizDto(list);
		if(null == bizDtoList || bizDtoList.isEmpty()) {
			bizDtoList=new ArrayList<>();
			//调拨
			if(StockCarTypeEnum.ALLOT.getValue() == stockCar.getType()) {
				if(StockCarStatusEnum.WAIT_STOCK.getValue() == stockCar.getStatus()) {
					StockRecordBizDto stockRecordBizDto = new StockRecordBizDto();
					BeanUtils.copyProperties(stockCar, stockRecordBizDto);
					stockRecordBizDto.setType(StockRecordTypeEnum.PURCHASE_IN_STORE.getValue());
					stockRecordBizDto.setStatus(StockRecordStatusEnum.WAIT_STOCK.getValue());
					stockRecordBizDto.setAccountAccount(null);
					stockRecordBizDto.setOperationTime(null);
					bizDtoList.add(stockRecordBizDto);
					bizDtoList.add(getStockRecordBizDto());
					return bizDtoList;
				}
			}
		}
		
		//转换自己的状态码
		getStockType(bizDtoList);
		
		//获取车辆当前状态
		if(StockCarTypeEnum.ALLOT.getValue() == stockCar.getType()) {
			if(StockCarStatusEnum.WAIT_STOCK.getValue() == stockCar.getStatus()) {
				StockRecordBizDto stockRecordBizDto = new StockRecordBizDto();
				BeanUtils.copyProperties(stockCar, stockRecordBizDto);
				stockRecordBizDto.setType(StockRecordTypeEnum.PURCHASE_IN_STORE.getValue());
				stockRecordBizDto.setStatus(StockRecordStatusEnum.WAIT_STOCK.getValue());
				stockRecordBizDto.setAccountAccount(null);
				stockRecordBizDto.setOperationTime(null);
				bizDtoList.add(stockRecordBizDto);
			}
			bizDtoList.add(getStockRecordBizDto());
		}
		
		//获取仓库名称
		getStockName(bizDtoList);
		return bizDtoList;	
	}
	
	/**
	 * 将车辆从中转库调拨门店 状态改为采购入店
	 * @param bizDtoList
	 */
	private void getStockType(List<StockRecordBizDto> bizDtoList) {
		for (StockRecordBizDto dto : bizDtoList) {
			if(StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == dto.getOutStockType()) {
				dto.setType(StockRecordTypeEnum.PURCHASE_IN_STORE.getValue());
			}
		}
	}
}
